"""
Case Type   : gs_checkse工具功能
Case Name   : 验证通过-h参数指定需要检查的节点ip
Create At   : 2025/02/13
Owner       : sungang
Description :
    1、gs_checkse -i A -f hosts (hosts为文件写入主机名称，hosts写入内容如下)
    ip1
    ip2
    ip3
    2、修复主机，备一、备二安全项gs_checkse -i B -f hosts
    3、当-f、-h参数同时使用合理报错gs_checkse -i B -h ip1,ip2,ip3 -f hosts
Expect      :
    1、主机、备一、备二安全项检测成功
    2、修复成功显示normal
    3、报错给出提示-h与-f参数不能同时使用
History     :
"""
import os
import unittest

from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.Common import CommonSH
from yat.test import Node
from yat.test import macro

pri_sh = CommonSH('PrimaryDbUser')


@unittest.skipIf(3 != pri_sh.get_node_num(), "需一主两备环境，否则不执行")
class GS_CHECKSE(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.sh_sta1 = Node('Standby1DbUser')
        self.sh_sta2 = Node('Standby2DbUser')
        self.user_node = Node('PrimaryDbUser')
        self.sh_sta1_ip = self.sh_sta1.ssh_host
        self.sh_sta2_ip = self.sh_sta2.ssh_host
        self.user_node_ip = self.user_node.ssh_host
        self.log.info(self.sh_sta1_ip, self.sh_sta2_ip, self.user_node_ip)
        self.common = Common()
        self.hosts_txt = 'hosts.txt'
        self.report_path = os.path.join(macro.DB_BACKUP_PATH, self.hosts_txt)
        text = '-----创建hosts文件，写入服务器信息,每个服务器信息单独占用一行-----'
        self.log.info(text)
        hosts_cmd = f"echo -e '{self.sh_sta1_ip}\n{self.sh_sta2_ip}\n{self.user_node_ip}' > {self.report_path}"
        self.log.info(hosts_cmd)
        sql_res = self.common.get_sh_result(self.user_node, hosts_cmd)
        self.log.info(sql_res)
        self.env_path = macro.DB_ENV_PATH

    def test_gscheckse(self):
        text = '-----step1:gs_checkse -i A -f hosts (hosts为主机写入文件名称）;expect:主机、备一、备二安全项检测成功-----'
        self.log.info(text)
        sql_cmd = f"source {self.env_path};gs_checkse -i A -f {self.report_path}"
        self.log.info(sql_cmd)
        sql_res = self.common.get_sh_result(self.user_node, sql_cmd)
        self.log.info(sql_res)
        self.assertIn("A1. [ Checking Connection configuration ]", sql_res, '执行失败' + text)
        self.assertIn(f"Total numbers:10. Abnormal numbers:", sql_res, '执行失败' + text)

        text = '-----step2:修复主机，备一、备二安全项gs_checkse -i B -f hosts;expect:修复成功显示normal-----'
        self.log.info(text)
        sql_cmd = f"source {self.env_path};gs_checkse -i B -f {self.report_path}"
        self.log.info(sql_cmd)
        sql_res = self.common.get_sh_result(self.user_node, sql_cmd)
        self.log.info(sql_res)
        self.assertIn("B1. [ Set Connection configuration ]", sql_res, '执行失败' + text)
        self.assertIn("Total numbers:10. Abnormal numbers:0. Warning numbers:0", sql_res, '执行失败' + text)

        text = '-----step3:当-f、-h参数同时使用合理报错gs_checkse -i B -h ip1,ip2,ip3 -f hosts;expect:检测备一ip2的安全项成功-----'
        self.log.info(text)
        sql_cmd = f"source {self.env_path};gs_checkse -i B -f {self.report_path} -h  {self.sh_sta1_ip},{self.sh_sta2_ip},{self.user_node_ip}"
        self.log.info(sql_cmd)
        sql_res = self.common.get_sh_result(self.user_node, sql_cmd)
        self.log.info(sql_res)
        self.assertIn(f"[GAUSS-50005] : The parameter '-h' and '-f' can not be used together", sql_res,
                      '执行失败' + text)

    def tearDown(self):
        step = '----清理保存环境信息文件----'
        self.log.info(step)
        del_file = f"rm -rf {self.report_path}"
        del_res = self.common.get_sh_result(self.user_node, del_file)
        self.assertEqual('', del_res, '执行失败：' + step)
        self.log.info(f'----{os.path.basename(__file__)} end----')
